package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.JingfeiDept;
import com.ruoyi.system.domain.JingfeiTuikuanjilu;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.impl.JingfeiDeptServiceImpl;
import com.ruoyi.system.service.impl.JingfeiTuikuanjiluServiceImpl;
import com.ruoyi.system.service.impl.JisuanServiceImpl;
import com.ruoyi.system.tools.BooleanMessage;
import com.ruoyi.web.controller.tool.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 部门信息
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/jisuan")
public class JisuanController extends BaseController
{

    @Autowired
    private JisuanServiceImpl jisuanService;


    @Autowired
    private JingfeiDeptServiceImpl jingfeiDeptService;

    @Autowired
    private JingfeiTuikuanjiluServiceImpl jingfeiTuikuanjiluService;

    @Log(title = "单位经费收款", businessType = BusinessType.UPDATE)
    @PostMapping("/shoukuan")
    @ResponseBody
    public JsonResult shoukuan(@RequestParam("id")String ppId, HttpServletRequest request) {

        try {
            JingfeiDept mm经费单位 = jingfeiDeptService.selectJingfeiDeptById(ppId);
            if(mm经费单位==null){
                return JsonResult.getErrorResult("当前经费单位记录不存在");
            }

            // 经费是否发放   用到的变动需要改为未变动
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            if(!deptid.equals(mm经费单位.getDeptid())){
                return JsonResult.getErrorResult("当前经费单位记录所属单位有误 操作失败");
            }
            if(mm经费单位.getIsshoukuan()==1){
                return JsonResult.getErrorResult("当前经费单位记录已收款 请勿重复操作");
            }

            BooleanMessage mm收款 = jisuanService.shoukuan(mm经费单位);
            if(mm收款.isOk()){
                return JsonResult.getSuccessResult("收款成功");
            }else{
                return  JsonResult.getErrorResult("收款失败");
            }
        } catch (Exception e) {
            logger.error("jisuan -> shoukuan: "+e.getMessage());
            return JsonResult.getErrorResult("单位收款失败");
        }
    }

    @Log(title = "单位经费发放", businessType = BusinessType.UPDATE)
    @PostMapping("/deptfafang")
    @ResponseBody
    public JsonResult Deptfafang(@RequestParam("piciid")String ppPiciid,@RequestParam("deptids") String ppDeptids,
                             @RequestParam("zhuanjiaids")String ppZhuanjiaids, HttpServletRequest request) {

        try {
            String[] mmdeptIds = ppDeptids.split(",");
            String[] mmzhuanjiaIds = ppZhuanjiaids.split(",");
            // 经费是否发放   用到的变动需要改为未变动
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            BooleanMessage mm发放 = jisuanService.Deptfafang(ppPiciid,deptid,mmdeptIds,mmzhuanjiaIds);
            if(mm发放.isOk()){
                return JsonResult.getSuccessResult("发放成功");
            }else{
                return  JsonResult.getErrorResult(mm发放.getMessage().toString());
            }
        } catch (Exception e) {
            logger.error("jisuan -> deptfafang: "+e.getMessage());
            return JsonResult.getErrorResult("单位经费发放失败");
        }
    }

    @Log(title = "专家中心经费发放", businessType = BusinessType.UPDATE)
    @PostMapping("/fafang")
    @ResponseBody
    public JsonResult fafang(@RequestParam("piciid")String ppPiciid,@RequestParam("deptids") String ppDeptids,
                             @RequestParam("zhuanjiaids")String ppZhuanjiaids, HttpServletRequest request) {

        try {
            String[] mmdeptIds = ppDeptids.split(",");
            String[] mmzhuanjiaIds = ppZhuanjiaids.split(",");
            // 经费是否发放   用到的变动需要改为未变动
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            BooleanMessage mm发放 = jisuanService.fafang(ppPiciid,deptid,mmdeptIds,mmzhuanjiaIds);
            if(mm发放.isOk()){
                return JsonResult.getSuccessResult("发放成功");
            }else{
                return  JsonResult.getErrorResult(mm发放.getMessage().toString());
            }
        } catch (Exception e) {
            logger.error("jisuan -> fafang: "+e.getMessage());
            return JsonResult.getErrorResult("专家中心经费发放失败");
        }
    }


    @Log(title = "单位经费发放列表", businessType = BusinessType.UPDATE)
    @PostMapping("/fafang_list")
    @ResponseBody
    public JsonResult fafang_list(@RequestParam("piciid")String ppPiciid,HttpServletRequest request) {

        try {
           HttpSession session = request.getSession();
           String deptid = session.getAttribute("deptid").toString();
           return JsonResult.getSuccessResult(jisuanService.fafanglist(ppPiciid,deptid));
        } catch (Exception e) {
            logger.error("jisuan -> fafang_list: "+e.getMessage());
            return JsonResult.getErrorResult("经费发放列表查询失败");
        }
    }

    @Log(title = "deptjingfeijisuan", businessType = BusinessType.INSERT)
    @PostMapping("/deptjingfeijisuan")
    @ResponseBody
    public JsonResult<?> deptjingfeijisuan(@RequestParam("piciid") String piciid, HttpServletRequest request)
    {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            BooleanMessage ok = jisuanService.DEPTJingfeijisuan(piciid,deptid);
            return ok.isOk()?JsonResult.getSuccessResult("计算成功"):JsonResult.getErrorResult(ok.getMessage().toString());
        } catch (RuntimeException e) {
            logger.error("jisuan -> deptjingfeijisuan: "+e.getMessage());
            return JsonResult.getErrorResult("经费计算失败");
        }
    }

    @Log(title = "tuikuan", businessType = BusinessType.INSERT)
    @PostMapping("/tuikuan")
    @ResponseBody
    public JsonResult<?> tuikuan(@RequestParam("ids") String ppids,@RequestParam("yuanyin")String yuanyin, HttpServletRequest request)
    {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            String[] mmIds = ppids.split(",");
            List<String> mmid集合 = Arrays.asList(mmIds);
            BooleanMessage ok = jisuanService.tuikuan(mmid集合,deptid,yuanyin);
            return ok.isOk()?JsonResult.getSuccessResult("计算成功"):JsonResult.getErrorResult(ok.getMessage().toString());
        } catch (RuntimeException e) {
            logger.error("jisuan -> tuikuan: "+e.getMessage());
            return JsonResult.getErrorResult("退款失败");
        }
    }

    // 查询每个单位自己可以用来发放的滞留记录
    @Log(title = "findzl", businessType = BusinessType.INSERT)
    @PostMapping("/findzl")
    @ResponseBody
    public JsonResult<?> findzl( HttpServletRequest request)
    {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            return JsonResult.getSuccessResult(jisuanService.findZLByDeptId(deptid));
        } catch (RuntimeException e) {
            logger.error("jisuan -> findzl: "+e.getMessage());
            return JsonResult.getErrorResult("查询本单位滞留经费失败");
        }
    }

    // 查询每个单位的退款和收款记录   isshoukuan =1 则查询本单位的收款记录 其他查本单位发起的退款 zhuangtai 是1 则是已收款 0是未受 其余全部
    @Log(title = "findtkjl", businessType = BusinessType.INSERT)
    @PostMapping("/findtkjl")
    @ResponseBody
    public JsonResult<?> findtkjl(@RequestParam("isshoukuan") String isshoukuan,@RequestParam("zhuangtai") String zhuangtai, HttpServletRequest request)
    {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            int mmzhuangtai = 99;
            if(zhuangtai.equals("1")||zhuangtai.equals("0")){
                mmzhuangtai =Integer.valueOf(zhuangtai);
            }
            String mmshoukuandeptid = "";
            String mmtuikuandeptid = "";
            if(isshoukuan.equals("1")){
                mmshoukuandeptid = deptid;
            }else{
                mmtuikuandeptid = deptid;
            }
            return JsonResult.getSuccessResult(jisuanService.findTKJLByDeptIdAndZhuangtai(mmtuikuandeptid,mmshoukuandeptid,mmzhuangtai));
        } catch (RuntimeException e) {
            logger.error("jisuan -> findtkjl: "+e.getMessage());
            return JsonResult.getErrorResult("查询退款记录失败");
        }
    }


    // 接受退款
    @Log(title = "tuikuanshoukuan", businessType = BusinessType.INSERT)
    @PostMapping("/tuikuanshoukuan")
    @ResponseBody
    public JsonResult<?> tuikuanshoukuan(@RequestParam("jiluid") String ppjiluid, HttpServletRequest request)
    {
        try {
            HttpSession session = request.getSession();
            String deptid = session.getAttribute("deptid").toString(); // 单位id
            // 查询经费退款记录 判断是否本单位
            JingfeiTuikuanjilu mmOne = jingfeiTuikuanjiluService.selectJingfeiTuikuanjiluById(ppjiluid);

            if(mmOne==null||!mmOne.getShoukuandeptid().equals(deptid)){
                return JsonResult.getErrorResult("收款失败");
            }
            BooleanMessage ok = jisuanService.tuikuanshoukuan(mmOne,deptid);
            return ok.isOk()?JsonResult.getSuccessResult("收款成功"):JsonResult.getErrorResult(ok.getMessage().toString());
        } catch (RuntimeException e) {
            logger.error("jisuan -> tuikuanshoukuan: "+e.getMessage());
            return JsonResult.getErrorResult("收款失败");
        }
    }

}
